package com.android.bookgarden.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.bookgarden.R;
import com.android.bookgarden.adapter.CityAdapter;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.ProvinceidBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.recycleview.BaseViewHolder;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by zouqh
 * on 2018/12/27
 **/
public class SelectCityActivity extends Activity implements RealtConstract.View {

    @BindView(R.id.recycler1)
    RecyclerView recycler1;
    @BindView(R.id.recycler2)
    RecyclerView recycler2;
    @BindView(R.id.sure)
    TextView sure;
    private ProvinceidBean provinceidBean;
    private RealtConstract.Presenter presenter;
    private List<ProvinceidBean> provinceidBeanList;
    private CityAdapter adapter1;
    private CityAdapter adapter2;
    private List<ProvinceidBean> cityList;
    private String proviceId;
    private String address;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        initView();
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null==provinceidBean){
                    showToast("请选择城市");
                }else{
                    if (null!=InputCityActivity.inputCityActivity){
                        InputCityActivity.inputCityActivity.setCity(provinceidBean,proviceId,address);
                   finish();
                    }
                }
            }
        });
    }

    protected int getLayoutId() {
        return R.layout.select_ciy_dialog;
    }


    protected void initView() {
        presenter = new BasePersenter(this, this);
        provinceidBeanList = new ArrayList<>();
        cityList = new ArrayList<>();
        adapter1 = new CityAdapter(provinceidBeanList, this);
        recycler1.setLayoutManager(new LinearLayoutManager(this));
        recycler1.setAdapter(adapter1);
        adapter1.setOnItemClickLitener(new CityAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, BaseViewHolder viewHolder) {
                provinceidBean =null;
                address= provinceidBeanList.get(position).getName();
                proviceId = provinceidBeanList.get(position).getId();
                adapter1.setIndex(position);
                getCity( proviceId+ "");
            }
        });
        adapter2 = new CityAdapter(cityList, this);
        recycler2.setLayoutManager(new LinearLayoutManager(this));
        recycler2.setAdapter(adapter2);
        adapter2.setOnItemClickLitener(new CityAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, BaseViewHolder viewHolder) {
                adapter2.setIndex(position);
                provinceidBean = cityList.get(position);
                address=address+","+provinceidBean.getName();
            }
        });
        getDataList();
    }

    private void getDataList() {
        presenter.loadDataHeader(null, "", Apis.GET_PROVICE, 1001, Utlis.getHeadler(this), HttpType.GET);
    }

    private void getCity(String id) {
        presenter.loadDataHeader(null, "", Apis.GET_CITY + id, 1002, Utlis.getHeadler(this), HttpType.GET);
    }




    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }
        LoadingDialog.hideProgress();
        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("请求失败");
        LoadingDialog.hideProgress();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {


                case 1001:
//                    refreshLayout.setRefreshing(false);
                    DataBean bean = Utlis.getDataBean(data);
                    if (null == bean) {
                        showToast("获取列表失败");
                        return;
                    }
                    if (bean.getCode() == 200) {
                        if (bean.getCode() == 200) {
                            List<ProvinceidBean> listD = JsonUitl.stringToList(bean.getData(), ProvinceidBean.class);
                            if (null == listD || listD.size() <= 0) {

                                return;
                            }
                            provinceidBeanList.addAll(listD);
                            adapter1.notifyDataSetChanged();

                        } else {
                            showToast(bean.getMessage() + "");
                        }


                        break;

                    } else {
                        showToast(bean.getMessage() + "");
                    }


                    break;

                case 1002:
//                    refreshLayout.setRefreshing(false);
                    bean = Utlis.getDataBean(data);
                    if (null == bean) {
                        showToast("获取列表失败");
                        return;
                    }
                    if (bean.getCode() == 200) {
                        if (bean.getCode() == 200) {
                            List<ProvinceidBean> listD = JsonUitl.stringToList(bean.getData(), ProvinceidBean.class);
                            if (null == listD || listD.size() <= 0) {

                                return;
                            }
                            cityList.clear();
                            cityList.addAll(listD);
                            adapter2.notifyDataSetChanged();

                        } else {
                            showToast(bean.getMessage() + "");
                        }


                        break;

                    } else {
                        showToast(bean.getMessage() + "");
                    }


                    break;


            }
        }
    };


    protected void showToast(String msg){
        Toast.makeText(this,""+msg,Toast.LENGTH_SHORT).show();
    }
}
